// import TableLyout from '@/components/commonPublic/tableLyoutDetail.vue'
import TableLyout from '@/components/commonPublic/tableLyout.vue'
import TableData from "@/components/public/el-table"; //主页面列表
import actions from "@/actions";
import messageBox from "@/components/public/messageBox"; //主页面分页
import CustomDialog from "@/components/materialDataManage/categoryManagement/index.vue";
import {
    getSpeciesDataList, //列表页接口
    deleteSpeciesDataById, //删除
    updateJLStatus, //启用/停用
} from "@/api/request.js";
import publicForm from "@/components/materialDataManage/fromCom.vue"; //弹框form
import elBreadcrumb from "@/components/public/el-breadcrumb.vue";
import formJs from "./regexDetail.js"; //新增弹框配置

export default {
    name: "categoryManagement",
    data() {
        return {
            name: "categoryManagement",
            expandRows: [], // 展开数组
            userNature: null, //用户Nature
            showMsgBox: false,
            msgTip: "请先选中数据行！",
            msgType: 1,
            isLock: false,
            projectId: null,
            projectName: null,
            loading: false,
            btnObj: formJs.btnObj, //按钮
            queryCriteriaData: {}, //分页
            navigatepageNums: null,
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            radio: "待办",
            // 列表
            getList: {
                page: 1,
                pageSize: 15,
                name: "", //材料种类/名称
                fVersion: "" //规格型号
            },
            pageSizes: [15, 30, 45, 60],
            isShow: false, //是否显示弹框

            statusNum: null, //点击那个按钮
            dataFid: null, //列表详情id
            currentRow: null, //列表详情
            activeIndex: "1",
            fId: null, //当前数据fId
            tableData: [],
            companyId: null,
            currentRowId: "",
            dialogTitle: null,
        };
    },
    computed: {
        hIndex() {
            // 当前页数 - 1 * 每页数据条数 + 1
            return (this.getList.page - 1) * this.getList.pageSize + 1
        }
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
        elBreadcrumb,
        CustomDialog,
        messageBox
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        this.yj_searchForm(this.getList, 1);

    },
    methods: {
        confirmMsg() {
            this.showMsgBox = false;
        },
        confirmText(status) {
            if (status == '删除种类') {
                this.msgTip = "此操作将永久删除该数据, 是否继续?";
                this.titelName = "删除种类";
            } else if (status == '删除材料') {
                this.msgTip = "此操作将永久删除该数据, 是否继续?";
                this.titelName = "删除材料";
            } 
            this.msgType = 2; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
            this.showMsgBox = true;

        },
        // 带功能确定
        confirmMsgs() {
            this.showMsgBox = false;
           if (this.titelName == "删除种类") {
                this.yj_delete();
            } else if (this.titelName == "删除材料") {
                this.yj_delete();
            }
        },
        // 页面切换
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            this.getList.page = val;
            this.yj_searchForm(this.getList);
        },
        refreshList(status) { //刷新列表
            if (status == 0) {
                this.isShow = true;
            }
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            })
        },
        closeDialog() {
            this.isShow = false;
        },
        handleRow() {},

        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status)
        },

        // 点击查看
        yj_viewForm(row, label) {
            if (label == "材料种类") {
                this.statusNum = 7
                this.isShow = true;
                this.currentRow = row;
                this.dialogTitle = '材料种类查看'


            } else if (label == "材料名称") {
                this.statusNum = 7
                this.isShow = true;
                this.currentRow = row;

                this.dialogTitle = '材料名称查看'
            }

            this.$router.push({
                path: "/materialDataManage/categoryManagementText",
            });
           var data = {
                data: this.currentRow,
                getList: this.getList,
                status: 7,
                title:this.dialogTitle
            }
            sessionStorage.setItem(
                "GxEmbedded_primaryInfo",
                JSON.stringify(data)
            );
        },
        // 新增/编辑
        yj_edit(num) {
            var data;
            this.statusNum = num
            num == 1 || num == 2 ? this.dialogTitle = '材料种类编辑' : num == 4 || num == 5 ? this.dialogTitle = '材料名称编辑' : this.dialogTitle = ""
            if (num == 0) { //启用/停用
                if (this.dataFid != null) {
                    if (this.currentRow.materialList) {
                        // 编辑
                        this.yj_istrue()
                    } else {
                        this.$message.warning({
                            content: '请先选择材料种类',
                            duration: 1000
                        })

                    }
                } else {
                    this.$message.warning({
                        content: '请选择需要改变状态的数据',
                        duration: 1000
                    })
                }
            } else if (num == 1) { //种类新增
                this.$router.push({
                    path: "/materialDataManage/categoryManagementText",
                });
                data = {
                    data: this.currentRow,
                    getList: this.getList,
                    status: num,
                    title:this.dialogTitle
                }
            } else if (num == 2) { //种类材料编辑
                if (this.dataFid != null) {
                    if (this.currentRow.materialList) {
                        // 编辑
                        // this.editText();
                        this.$router.push({
                            path: "/materialDataManage/categoryManagementText",
                        });
                        data = {
                            data: this.currentRow,
                            getList: this.getList,
                            status: num,
                            title:this.dialogTitle
                        }
                    } else {
                        this.$message.warning({
                            content: '请选择材料种类',
                            duration: 1000
                        })
                    }
                } else {
                    this.$message.warning({
                        content: '请先选择需要编辑的数据',
                        duration: 1000
                    })
                }
            } else if (num == 4) { //材料新增
                if (this.dataFid != null) {
                    // 编辑
                    this.$router.push({
                        path: "/materialDataManage/categoryManagementText",
                    });
                    data = {
                        data: this.currentRow,
                        getList: this.getList,
                        status: num,
                        title:this.dialogTitle
                    }
                } else {
                    this.$message.warning({
                        content: '请先选择种类',
                        duration: 1000
                    })
                }
            } else if (num == 5) { //种类材料编辑
                if (this.dataFid != null) {
                    if (!this.currentRow.materialList) {
                        // 编辑
                        this.$router.push({
                            path: "/materialDataManage/categoryManagementText",
                        });
                        data = {
                            data: this.currentRow,
                            getList: this.getList,
                            status: num,
                            title:this.dialogTitle
                        }
                    } else {
                        this.$message.warning({
                            content: '请先选择需要编辑的材料',
                            duration: 1000
                        })
                    }
                } else {
                    this.$message.warning({
                        content: '请先选择需要编辑的数据',
                        duration: 1000
                    })
                }
            } else if (num == 3 || num == 6) { //种类材料删除
                if (this.dataFid != null) {
                    // this.yj_delete()
                    
                    if(num==3){
                        this.confirmText('删除种类');
                    }else {
                        this.confirmText('删除材料');
                    }
                } else {
                    this.$message.warning({
                        content: '请先选择需要删除的数据',
                        duration: 1000
                    })
                }
            } else if (num == 7) { //查看
                if (this.dataFid != null) {
                    if (this.currentRow.materialList) {
                        this.dialogTitle = '材料种类查看'
                    } else {
                        this.dialogTitle = '材料名称查看'
                    }
                    this.statusNum = 7
                    this.$router.push({
                        path: "/materialDataManage/categoryManagementText",
                    });
                    data = {
                        data: this.currentRow,
                        getList: this.getList,
                        status: num,
                        title:this.dialogTitle
                    }

                } else {
                    this.$message.warning({
                        content: '请先选择需要查看的数据',
                        duration: 1000
                    })
                }
            }
            sessionStorage.setItem(
                "GxEmbedded_primaryInfo",
                JSON.stringify(data)
            );
        },
   


        // 查看

        // 点击当前行变色
        rowChange(val, expandedRows) {
            this.currentRow = val
            this.dataFid = val.fId;
            if (expandedRows.length > 1) {
                this.$refs.chartTable.toggleRowExpansion(expandedRows[0])
            }
        },
        // 获取列表数据
        yj_searchForm(data) {
            var obj = {
                page: data.page,
                pageSize: 15
            }
            this.loading = true;
            var that = this;
            getSpeciesDataList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                        that.navigatepageNums =
                            res.data.pageInfo.navigatepageNums.length;
                    } else {
                        this.$message.error({
                            content: '列表数据获取失败' + res.msg,
                            duration: 1000
                        })
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error({
                        content: '列表数据获取失败' + error.msg,
                        duration: 1000
                    })

                    this.loading = false;
                });
        },
        // 删除
        yj_delete() {
            if (this.dataFid != "" && this.dataFid != null) {
                this.loading = true;
                this.isLock = true
                deleteSpeciesDataById({
                        fId: this.dataFid,
                    })
                    .then((res) => {
                        if (res.errcode == 0) {
                            this.isLock = false
                            this.loading = false;
                            this.yj_searchForm(this.getList);
                            this.$message.success({
                                content: "删除成功",
                                duration: 1000
                            });
                            this.dataFid = null

                        } else {
                            this.isLock = false
                            this.$message.error({
                                content: res.data,
                                duration: 1000
                            });
                            this.loading = false;
                        }
                    })
            } else {
                this.$message.error({
                    content: "请先选择需要删除的数据",
                    duration: 1000
                });
            }
        },
        // 启用/停用
        yj_istrue() {
            this.loading = true;
            updateJLStatus({
                    fId: this.dataFid,
                })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.isLock = false
                        this.loading = false;
                        this.yj_searchForm(this.getList);
                        this.$message.success({
                            content: "状态更改成功",
                            duration: 1000
                        });
                        // this.dataFid = null

                    } else {
                        this.isLock = false;
                        this.$message.error({
                            content: res.data,
                            duration: 1000
                        });
                        this.loading = false;
                    }
                })
        },
    },
};